<?php

namespace app\web\controller;

use app\common\model\Activity as ActivityModel;
use app\common\model\ActivityItem;
use app\common\model\ActivityItemViper;
use app\common\service\ActivityService;
use app\web\validate\ActivityValidate;
use think\Db;

/**
 * 设置活动
 * Class Combine
 * @package app\manage\admin
 */
class Activity extends Auth
{

    /**
     * 活动列表
     * @return mixed
     * @throws \think\Exception
     */
    public function index()
    {
        if ($this->request->isPost()) {
            $limit = $this->request->post('limit') ??10;
            // 表单数据
            $data_list = ActivityModel::order("id desc")->paginate($limit);
            foreach ($data_list as $key => &$value) {
                $value['activity_item'] = ActivityItem::where('activity_id' , $value['id'])->column('name') ?? [];
                $value['updater_name'] = Db::name('qt_users')->where('id', $value['updater'])->value('real_name') ?? '';
            }
            $this->result('查询成功', $data_list, 200);
        }
        $this->result('无效的请求', '', 204);

    }

    /**
     * 添加活动
     * @return mixed
     * @throws \think\Exception
     */
    public function add()
    {
        //判断是否提交信息
        if ($this->request->isPost()) {
            // 表单数据
            $post = $this->request->post();

            $validate = new ActivityValidate();
            if (!$validate->scene('add')->check($post)) {
                $this->result($validate->getError(), '', 204);
            }


//            $info = ActivityModel::where('name', $post['name'])->find();
//            if (!empty($info)) $this->result('活动名已经存在', '', 204);
            /*$post['activity_item'] =  [
                [
                    'item'=>[
                        'item_id'=>'1',//项目/商品id
                        'item_type'=>'goods',//goods,project
                        'name'=>'商品名称',//项目或者商品名
                        'price'=>'500',//原价
                        'activity_price'=>'400',//活动价
                        'add_block_price'=>'300',//加钟价
                    ],
                    'viper'=>[
                        [
                            //'activity_item_id'=>'',//itemID
                            'viper_card_type_id'=>'1',//会员卡类型
                            'viper_price'=>'300',//会员价
                            'viper_add_block_price'=>'200',//会员加钟价]
                        ],
                        [
                            //'activity_item_id'=>'',//itemID
                            'viper_card_type_id'=>'2',//会员卡类型
                            'viper_price'=>'280',//会员价
                            'viper_add_block_price'=>'250',//会员加钟价]
                        ]
                    ]
                ],
                [
                    'item'=>[
                        'item_id'=>'1',//项目/商品id
                        'item_type'=>'project',//goods,project
                        'name'=>'项目名称',//项目或者商品名
                        'price'=>'500',//原价
                        'activity_price'=>'400',//活动价
                        'add_block_price'=>'300',//加钟价
                    ],
                    'viper'=>[
                        [
                            //'activity_item_id'=>'',//itemID
                            'viper_card_type_id'=>'1',//会员卡类型
                            'viper_price'=>'300',//会员价
                            'viper_add_block_price'=>'200',//会员加钟价]
                        ],
                        [
                            //'activity_item_id'=>'',//itemID
                            'viper_card_type_id'=>'2',//会员卡类型
                            'viper_price'=>'280',//会员价
                            'viper_add_block_price'=>'250',//会员加钟价]
                        ]
                    ]
                ],
            ];*/
            $post['date_start'] = strtotime($post['date_start']);
            $post['date_end'] = strtotime($post['date_end']);
            $user_info = $this->userInfo;
            $post['store_id'] = $user_info['store_id'];
            $post['updater'] = $post['creater'] = $user_info['id'];
            if ($model = ActivityModel::create($post)) {
                if (!empty($post['activity_item'])) {
                   $result =  ActivityService::saveActivityItem($model['id'],json_decode($post['activity_item'],true));
                   if($result['status'] == false){
                       $this->result($result['msg'], '', 204);
                   }
                }

                $this->result('新增成功', '', 200);
            } else {
                $this->result('新增失败', '', 204);
            }
        }

        $this->result('请求错误', '', 204);
    }


    /**
     * 编辑活动
     * @param null $id
     * @return mixed
     * @throws \think\Exception
     */
    public function edit()
    {
        //判断是否提交信息
        if ($this->request->isPost()) {
            // 表单数据
            $post = $this->request->post();
            $info = ActivityModel::where('id','<>',$post['id'])->where('name', $post['name'])->find();
            if ($post['id'] != $info['id'] && !empty($info)){
                $this->result('活动名已经存在', '', 204);
            }
            $post['date_start'] = strtotime($post['date_start']);
            $post['date_end'] = strtotime($post['date_end']);
            $user_info = $this->userInfo;
            $post['store_id'] = $user_info['store_id'];
            $post['updater'] = $user_info['id'];
            if ($model = ActivityModel::update($post)) {

                if (!empty($post['activity_item'])) {
                    $item_ids = Db::name("qt_data_activity_item")->where(['activity_id'=>$model['id']])->column('id');
                    Db::name("qt_data_activity_item")->where(['activity_id'=>$model['id']])->delete();
                    Db::name("qt_data_activity_item_viper")->whereIn('activity_item_id',$item_ids)->delete();
                    ActivityService::saveActivityItem($model['id'],json_decode($post['activity_item'],true));
                }

                $this->result('编辑成功', '', 200);
            } else {
                $this->result('编辑失败', '', 204);
            }
        }

        $this->result('请求错误', '', 204);
    }


    /**
     * 编辑获取活动详情
     */
    public function getActivityInfo () {

        if ($this->request->isPost()) {
            // 表单数据
            $post = $this->request->post();
            if (empty($post['id'])) {
                $this->result('ID必填', '', 204);
            }
            $id = $post['id'];

            $activity_data = ActivityModel::where('id', $id)->find();
            if (empty($activity_data)) {
                $this->result('没有查询到对应ID的活动数据', '', 204);
            }

            $item_data = ActivityItem::where('activity_id', $id)->select();
            if (empty($item_data)) {
                $this->result('没有查询到对应ID的活动详情数据', '', 204);
            }

            $item_ids = array_column($item_data->toArray(),'id');
            $viper_data = ActivityItemViper::whereIn('activity_item_id', $item_ids)->select();
            $data['activity_data'] =  $activity_data;
            $data['item_data'] =  $item_data;
            $data['viper_data'] =  $viper_data;
            $this->result('查询成功', $data, 200);
        } else {
            $this->result('请求错误', '', 204);
        }


    }

    /**
     * 删除接口
     */
    public function remove () {

        if ($this->request->isPost()) {
            // 表单数据
            $post = $this->request->post();
            if (empty($post['id'])) {
                $this->result('ID必填', '', 204);
            }
            Db::name("qt_data_activity")->where(['id'=>$post['id']])->delete();

            $item_ids = Db::name("qt_data_activity_item")->where(['activity_id'=>$post['id']])->column('id');
            Db::name("qt_data_activity_item")->where(['activity_id'=>$post['id']])->delete();

            Db::name("qt_data_activity_item_viper")->whereIn('activity_item_id',$item_ids)->delete();

            $this->result('删除成功', '', 200);
        } else {
            $this->result('请求错误', '', 204);
        }
    }

}